﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class UserPrivDao
    {
        public bool Delete(SqlConnection conn, int privId)
        {
            string commandText = "DECLARE @RET BIT IF EXISTS(SELECT [UserId] FROM [dbo].[Users] WHERE [PrivId] = @PrivId) SET @RET = 0 ELSE BEGIN DELETE FROM [dbo].[User_Priv] WHERE [PrivId] = @PrivId SET @RET = 1 END SELECT @RET";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@PrivId", SqlDbType.Int) };
            commandParameters[0].Value = privId;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            return ((obj2 != null) && Convert.ToBoolean(obj2));
        }

        public int Insert(SqlConnection conn, UserPrivData data)
        {
            string commandText = "INSERT INTO [dbo].[User_Priv] ( [PrivNo], [PrivName], [FuncIdStr] ) VALUES ( @PrivNo, @PrivName, @FuncIdStr ) SELECT SCOPE_IDENTITY() AS RetValue";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@PrivNo", SqlDbType.Int);
            commandParameters[0].Value = data.PrivNo;
            commandParameters[1] = new SqlParameter("@PrivName", SqlDbType.NVarChar, 200);
            commandParameters[1].Value = data.PrivName;
            commandParameters[2] = new SqlParameter("@FuncIdStr", SqlDbType.NText);
            if (data.FuncIdStrIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.FuncIdStr;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            string commandText = "SELECT [PrivId], [PrivName], [PrivNo], [FuncIdStr] FROM [dbo].[User_Priv] ORDER BY [PrivNo] ASC";
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "User_Priv";
            return set.Tables[0];
        }

        public DataTable SelectByPrivId(SqlConnection conn, int privId)
        {
            string commandText = "SELECT [PrivId], [PrivName], [PrivNo], [FuncIdStr] FROM [dbo].[User_Priv] WHERE [PrivId] = @PrivId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@PrivId", SqlDbType.Int) };
            commandParameters[0].Value = privId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "User_Priv";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, UserPrivData data)
        {
            string commandText = "UPDATE [dbo].[User_Priv] SET [PrivName] = @PrivName, [PrivNo] = @PrivNo, [FuncIdStr] = @FuncIdStr WHERE [PrivId] = @PrivId";
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@PrivId", SqlDbType.Int);
            commandParameters[0].Value = data.PrivId;
            commandParameters[1] = new SqlParameter("@PrivNo", SqlDbType.Int);
            commandParameters[1].Value = data.PrivNo;
            commandParameters[2] = new SqlParameter("@PrivName", SqlDbType.NVarChar, 200);
            commandParameters[2].Value = data.PrivName;
            commandParameters[3] = new SqlParameter("@FuncIdStr", SqlDbType.NText);
            if (data.FuncIdStrIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.FuncIdStr;
            }
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }
    }
}

